﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using NS_DBOP;
using System.Data.SqlClient;

public partial class sysManage_NewManage : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            Bind();
        }
    }

    public void Bind()
    {
        
        DataSet ds = new DBoperate().ExecuteQuery("select * from News",0,0,"table1");
        DataTable dt = new DataTable();
        dt = ds.Tables[0];
        DataColumn dc = dt.Columns.Add("number", System.Type.GetType("System.String"));
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            dt.Rows[i]["number"] = (i + 1).ToString(); 
        }

        DataView dv = ds.Tables[0].DefaultView;
        PagedDataSource pds = new PagedDataSource();
        AspNetPager1.RecordCount = dv.Count;
        pds.DataSource = dv;
        pds.AllowPaging = true;
        pds.CurrentPageIndex = AspNetPager1.CurrentPageIndex - 1;
        pds.PageSize = AspNetPager1.PageSize;
        this.GridView1.DataSource = pds;
        this.GridView1.DataBind();
    }
    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
        Bind();
    }
    protected void chkAll_CheckedChanged(object sender, EventArgs e)
    {
        foreach (GridViewRow row in GridView1.Rows)
        {

            CheckBox ckTemp = (CheckBox)row.FindControl("chkDo");
            if (ckTemp != null)
            {
                if (chkAll.Checked)
                {
                    ckTemp.Checked = true;
                }
                else
                {
                    ckTemp.Checked = false;
                }
            }
            else
            {
                Response.Write("<script language=javascript>alert('没有值');</script>");

            }

        }
    }
    protected void btnDel_Click(object sender, EventArgs e)
    {

        //if (Convert.ToInt32(Session["Purview"]) > 5)
        //{
            try
            {
                sysDelete sd = new sysDelete();
                bool blSelected = false;
                int j = this.GridView1.Rows.Count;
                for (int i = 0; i < j; i++)
                {
                    CheckBox ckTemp = (CheckBox)GridView1.Rows[i].FindControl("chkDo");
                    if (ckTemp.Checked)
                    {
                        blSelected = true;
                        int strId = Convert.ToInt32(this.GridView1.DataKeys[i]["NewsID"].ToString());

                        string strDel = "delete from News where NewsID=" + strId;
                        int result = new DBoperate().ExecuteUpdate(strDel);


                    }
                }
                if (blSelected == false)
                {
                    Response.Write("<script language=javascript>alert('请先选择一条要操作的信息!');</script>");
                }

                Bind();//重新绑定数据
            }
            catch 
            {
                Response.Write("<script language=javascript>alert('错误！');</script>");
                //Response.Write(ex.Message);
            }
        //}
        //else
        //{
        //    Response.Write("<script language=javascript>alert('对不起,您没有此权限！！');</script>");
        //}

            
        
    }
    public string getType(string id)
    {
        string type = new DBoperate().ExecuteScalar("select Type from NewsType where NTID=" + Convert.ToInt32(id)).ToString();
        return type;
    }

    public string getUser(string id)
    {
        string user = new DBoperate().ExecuteScalar("select Name from Employee where Employeeid=" + Convert.ToInt32(id)).ToString();
        return user;
    }
}
